package com.example.springbooti18.init;

import com.example.springbooti18.entity.Message;
import com.example.springbooti18.service.MessageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 数据库消息初始化器 - 不依赖properties文件，直接在代码中定义国际化消息
 */
@Component
public class DbMessageInitializer implements ApplicationRunner {

    private static final Logger logger = LoggerFactory.getLogger(DbMessageInitializer.class);

    private final MessageService messageService;

    public DbMessageInitializer(MessageService messageService) {
        this.messageService = messageService;
    }

    @Override
    public void run(ApplicationArguments args) {
        logger.info("开始初始化数据库国际化消息...");

        // 检查是否已存在消息数据
        List<String> languages = messageService.getAllLanguageCodes();
        if (!languages.isEmpty()) {
            logger.info("数据库中已存在国际化消息数据，跳过初始化");
            return;
        }

        // 创建基本的国际化消息数据
        List<Message> messages = new ArrayList<>();

        // 添加中文消息
        messages.add(createMessage("welcome", "zh_CN", "欢迎使用Spring Boot国际化演示", "欢迎消息"));
        messages.add(createMessage("hello", "zh_CN", "你好，世界！", "问候语"));
        messages.add(createMessage("change.language", "zh_CN", "切换语言", "切换语言按钮"));
        messages.add(createMessage("current.language", "zh_CN", "当前语言：中文", "当前语言显示"));
        messages.add(createMessage("database.source", "zh_CN", "消息来源：数据库", "消息来源显示"));
        messages.add(createMessage("welcome.description", "zh_CN", "这是一个完全基于数据库的国际化演示应用", "描述文本"));
        messages.add(createMessage("api.test", "zh_CN", "API测试", "API测试链接文本"));

        // 添加英文消息
        messages.add(createMessage("welcome", "en_US", "Welcome to Spring Boot i18n Demo", "Welcome message"));
        messages.add(createMessage("hello", "en_US", "Hello, World!", "Greeting"));
        messages.add(createMessage("change.language", "en_US", "Change Language", "Language switch button"));
        messages.add(createMessage("current.language", "en_US", "Current Language: English", "Current language display"));
        messages.add(createMessage("database.source", "en_US", "Message Source: Database", "Message source display"));
        messages.add(createMessage("welcome.description", "en_US", "This is a fully database-based i18n demo application", "Description text"));
        messages.add(createMessage("api.test", "en_US", "API Test", "API test link text"));

        // 保存消息到数据库
        try {
            messageService.saveMessages(messages);
            logger.info("成功初始化 {} 条国际化消息到数据库", messages.size());
        } catch (Exception e) {
            logger.error("初始化国际化消息失败：{}", e.getMessage());
            e.printStackTrace();
        }
    }

    private Message createMessage(String key, String languageCode, String value, String description) {
        Message message = new Message();
        message.setKey(key);
        message.setLanguageCode(languageCode);
        message.setValue(value);
        message.setDescription(description);
        return message;
    }
}